package com.ooqiu.gaming.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ooqiu.gaming.GamingApp;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.injector.components.AppComponent;
import com.ooqiu.gaming.injector.modules.ActivityModule;
import com.ooqiu.gaming.utils.ResourceUtil;
import com.ooqiu.gaming.utils.StatusBarUtil;
import com.ooqiu.gaming.view.stateview.StateView;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.umeng.analytics.MobclickAgent;


/**
 * @author Eenie
 */
public abstract class BaseActivity extends AppCompatActivity implements StateView.OnRetryClickListener {

    protected Context mContext;
    protected StateView mStateView;
    protected View statusView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        initTheme();
        super.onCreate(savedInstanceState);
        setContentView(initContentView());
        mContext = this;
        setTranslucentStatus(isApplyStatusBarTranslucency());
        setStatusBarColor(isApplyStatusBarColor(), getStateBarDrawable());
        ARouter.getInstance().inject(this);
        initInjector();
        setToolbar(getToolbar());
        mStateView = initStatusLayout(statusLayoutId());
        if (mStateView != null) {
            mStateView.setOnRetryClickListener(this);
        }
        initUiAndListener();
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
    }

    private StateView initStatusLayout(View view) {
        if (view == null) {
            return StateView.inject(this, true);
        } else {
            return StateView.inject(view, false);
        }
    }

    private void setToolbar(Toolbar toolbar) {
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
    }


    private void initTheme() {
        int theme;
        try {
            theme = getPackageManager().getActivityInfo(getComponentName(), 0).theme;
        } catch (PackageManager.NameNotFoundException e) {
            return;
        }
//        if (theme != R.style.AppThemeLaunch) {
//            theme = ThemeUtil.themeArr[SettingPrefUtil.getThemeIndex(this)][
//                    SettingPrefUtil.getNightModel(this) ? 1 : 0];
//        }
        setTheme(theme);
    }

    /**
     * 设置Layout 如 R.layout.main_layout
     */
    public abstract int initContentView();

    /**
     * 注入Injector
     */
    public abstract void initInjector();

    /**
     * init UI && Listener
     */
    public abstract void initUiAndListener();

    /**
     * is applyStatusBarTranslucency
     */
    protected abstract boolean isApplyStatusBarTranslucency();


    protected abstract boolean isApplyStatusBarColor();

    private Toolbar getToolbar() {
        return toolbarIdRes();
    }

    protected abstract Toolbar toolbarIdRes();

    protected abstract View statusLayoutId();


    /**
     * set status bar translucency
     */
    protected void setTranslucentStatus(boolean on) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            if (on) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
            }
            win.setAttributes(winParams);
        }
    }


    /**
     * use SystemBarTintManager
     */
    public void setStatusBarColor(boolean on, Drawable drawable) {
        if (on) {
//            StatusBarUtil.setColor(this, ResourceUtil.getThemeColor(this), 0);


            if (drawable != null) {
                statusView = StatusBarUtil.setDrawable(this, drawable);
            } else {
                statusView = StatusBarUtil.setDrawable(this, getResources().getDrawable(R.drawable.shape_statuebar));
            }

        }
    }

    /**
     * 重载Activity
     */
    public void reload() {
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        return ResourceUtil.getStatusBarHeight(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (getSupportParentActivityIntent() != null) {
                startActivity(getSupportParentActivityIntent());
            }
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public ARouter getRouter() {
        return ARouter.getInstance();
    }


    protected void showLoading() {
        mStateView.showLoading();
    }

    protected void showContent() {
        mStateView.showContent();
    }

    protected void showEmpty() {
        mStateView.showEmpty();
    }

    protected void showRetry(String msg) {
        mStateView.showRetry(msg);
    }

    protected void showRetry() {
        mStateView.showRetry();
    }


    protected AppComponent getApplicationComponent() {
        return ((GamingApp) getApplication()).getApplicationComponent();
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    public Drawable getStateBarDrawable() {
        return null;
    }


    @Override
    public void onRetryClick() {

    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName() + pageExtra());
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName() + pageExtra());
        MobclickAgent.onPause(this);
    }


    protected String pageExtra() {
        return "";
    }


    public View getStatusView() {
        return statusView;
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            getStatusView().setVisibility(View.VISIBLE);
        } else {
            getStatusView().setVisibility(View.GONE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
        }



    }
}
